<?php

namespace App\Models;

use App\Models\Field\DynamicField;

/**
 * App\Models\SysDynamic
 * This is the model class for table "sys_dynamic_".
 *
 * @property int $id 自增ID
 * @property string $dynamic_id 动态ID
 * @property string $user_id 用户ID
 * @property string $target_id 目标ID
 * @property int $target_type 目标type 1文章 2笔记
 * @property int $type 动态类型 1原创文章动态，2笔记动态，3用户推荐文章动态，4话题动态
 * @property int $is_view 是否全部用户可见，0否1是
 * @property int $is_deleted 是否删除，0正常，1删除
 * @property int $created_at 创建时间戳
 * @property int $updated_at 更新时间戳
 *
 * @property SysArticle $article
 * @property SysNote $note
 * @property IndexDynamic $index
 *
 * @method static \Illuminate\Database\Eloquent\Builder|SysDynamic newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|SysDynamic newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|SysDynamic query()
 */
class SysDynamic extends BaseModel
{

    protected $table = "sys_dynamic_";
    protected $primaryKey = "dynamic_id";
    protected $keyType = "string";
    public $timestamps = true;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'created_at' => 'timestamp',
        'updated_at' => 'timestamp'
    ];

    /**
     * select columns
     * @var array
     */
    public $fields = [
        'user' => ['user_id', 'nickname', 'avatar', 'user_id as id'],
        'article' => ['article_id', 'cover_url', 'title', 'description', 'is_deleted', 'is_shield', 'created_at', 'article_id as id'],
        'note' => ['note_id', 'user_id', 'article_id', 'is_choice', 'is_deleted', 'is_public', 'is_shield', 'created_at', 'note_id as id'],
    ];

    /**
     * 关联模型 - 用户
     * @return \Illuminate\Database\Eloquent\Relations\hasOne
     */
    public function user()
    {
        return $this->hasOne(User::class, 'user_id', 'user_id')
            ->select($this->fields['user']);
    }

    /**
     * 关联模型 - 文章
     * @return \Illuminate\Database\Eloquent\Relations\hasOne
     */
    public function article()
    {
        $relation = $this->getRelationAttributes();
        return $this->hasOne(SysArticle::class, 'article_id', 'target_id', $relation->target_id)
            ->select($this->fields['article']);
    }

    /**
     * 关联模型 - 笔记
     * @return \Illuminate\Database\Eloquent\Relations\hasOne
     */
    public function note()
    {
        $relation = $this->getRelationAttributes();
        return $this->hasOne(SysNote::class, 'note_id', 'target_id', $relation->target_id)
            ->select($this->fields['note']);
    }

    /**
     * 模型属性 - 文章
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getArticleAttribute()
    {
        return SysArticle::getInstance($this->target_id)
            ->first();
    }

    /**
     * 模型属性 - 笔记
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getNoteAttribute()
    {
        return SysNote::getInstance($this->target_id)->first();
    }

    public function getIndexAttribute()
    {
        return IndexDynamic::getInstance()->where('dynamic_id', $this->dynamic_id)->first();
    }

    /**
     * 格式化字段
     * @param string $scene
     * @return array
     */
    public function commonFormat($scene = DynamicField::BASE_INFO): array
    {
        $formats = DynamicField::getFormats($scene);
        return DynamicField::format($this, $formats);
    }
}
